02. Running Code Locally
Running Code Locally
If you haven't already tried running your C++ code locally, now's the time to get things working. In the C++ section, we mentioned how to get code to run locally on your computer. Here are the instructions again for your reference:
Here are some suggested programs for writing and executing your code locally:
- Sublime Text as a general text editor
- Download an IDE (Integrated Development Environment) such as Xcode (Mac only), Visual Studio or any other IDE.
Detailed Setup Instructions
Big thanks to Michael Ikemann! Michael is a student in this Nanodegree and put together some exceptionally detailed documentation to help you get started with C++.
Below you'll find further instructions as well, although Michael's documents should be enough to get things running.
Compiling and Executing C++ Code
If you are on a Windows, Mac, or Linux machine, the basic process is going to be the same; you will compile your code and then execute the compiled code. But the details of how to do this are slightly different on different machines and operating systems.
And there are two different ways you can compile and execute your code:
- using the windows console/mac terminal/linux terminal
- using an IDE (Integrated Development Environment), which is a software program that makes it easier to write, organize, compile and run your code.
Running your Code through the Console or Terminal
This is the quickest way to get started compiling and executing your code.
For Windows
You will first need to download and install the Visual C++ Build Tools, which are provided by Microsoft. The installation process might vary depending on what version of windows you are using. The microsoft website has a helpful guide about how to make sure the installation went correctly. The guide also explains how to compile and run your code: microsoft instructions.
If the Visual C++ Build Tools are installed, you should be able to compile and execute your code from the console. For example, if you have a main.cpp, you would open the console and navigate to the directory with the file.
You will see that the microsoft tutorial suggests compiling your code with the following command:
cl /W4 /EHsc main.cpp
The /W4 and /EHsc are options that will compile your code with warnings and error handing.
The compiler outputs an .obj file, which contains the instructions for the CPU. You will also see a .exe file that will get your code running.
Now typing
main
at the command prompt should get your program running.
If your program has multiple .cpp files, then you compile with:
cl /W4 /EHsc file1.cpp file2.cpp file3.cpp
The name of the .obj and .exe files will be file1.
For Mac
Depending on your OS version, the installation process will be different. In general, you will need to download the "command line developer tools" from the Apple developer website.
As a first step, see if you already have a compiler on your system called g++. Open the Terminal application, and type
g++
If g++ is installed, you should get an error message like no input files
. If g++ is not installed and you are using a relatively newer version of Mac OS, a dialogue box will pop up asking you if you would like to install the command line tools. Click "Install".
However, on older Mac OS systems, you'll need to go to the Apple developer's website and download the command line developer tools. Go to this link. You will need to create an Apple ID if you do not already have one. Enter your Apple ID and password.
On the downloads page, there is a search box in the top left corner. Search for "command line developer tools". Then download and install the developer tools for your OS version.
Now go back to the Terminal and type
g++
You should now get an error message no input files
.
To compile a program you would type:
g++ filename.cpp
or with multiple .cpp files:
g++ filename1.cpp filename2.cpp filename3.cpp
The compiler will create an executable file named a.out
. To run your program, in terminal type:
./a.out
For Linux Ubuntu
The g++ compiler might already be on your system. If you are using Ubuntu, open Terminal and type:
dpkg --list | grep compiler
You will see a list of compilers. Check if g++ is in the list. If not, you can install g++ by typing:
sudo apt-get install g++
The commands to compile and run a program are the same as for Mac:
g++ filename.cpp
./a.out
Using an IDE
Another option is to download an IDE (Integrated Development Environment) to help write, organize, debug, compile and execute your code.
Microsoft provides an IDE called Visual Studio, which you can download here: Visual Studio. Up until recently, Visual Studio was only available for Windows. But there is also a Mac version as well.
For Mac users, Apple also provides an IDE called Xcode, which you can download here.
You can use either of these IDEs to help you develop your C++ programs.
For Linux Ubuntu users, there are a number of free IDEs available such as NetBeans, Code::Blocks, Eclipse, and CodeLite.